/**
 * 
 */
package com.xy.frame.service;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.xy.commons.PageUtil;
import com.xy.frame.dao.MenuMapper;

/**
 * @author admin
 * 
 */
@Service
public class MenuService {
	@Autowired
	private MenuMapper menuMapper;

	/***
	 * 加载所有的菜单
	 */
	public List<Map<String, Object>> loadMenuTree() {
		List<Map<String, Object>> list = menuMapper.loadMenuTree();
		// 转换成树形结构
		List<Map<String, Object>> treeList = Lists.newArrayList();
		for (Map<String, Object> map : list) {
			// 0 1 2 3 4
			if (map.get("F_MENU_LEVEL").toString().equals("0")) {
				Map<String, Object> node1 = Maps.newHashMap();
				node1.putAll(map);
				node1.put("child", getChild(Lists.newArrayList(list), map));
				treeList.add(node1);
			}
		}
		return treeList ;
	}

	/**
	 * 获取当前节点的所有子节点
	 * 
	 * @param listcopy
	 * @param currentNode
	 * @return
	 */
	private List<Map<String, Object>> getChild(List<Map<String, Object>> listcopy, Map<String, Object> currentNode) {
		List<Map<String, Object>> subList = Lists.newArrayList();
		String menuNum = currentNode.get("F_MENU_NUM").toString();
		String menuLevel = currentNode.get("F_MENU_LEVEL").toString();
		for (Map<String, Object> map : listcopy) {
			Map<String, Object> subMap = Maps.newHashMap(map);
			String parentMenuNum = subMap.get("F_MENU_PARENT") != null ? subMap.get("F_MENU_PARENT").toString() : "";
			if (parentMenuNum.equals(menuNum)) {
				String subMenuLevel = subMap.get("F_MENU_LEVEL").toString();
				if ((Integer.valueOf(menuLevel).intValue() + 1) == Integer.valueOf(subMenuLevel)) {
					subMap.put("child", getChild(Lists.newArrayList(listcopy), subMap));
				}
				subList.add(subMap);
			}
		}
		return subList;
	}

	/**
	 * @param params
	 * @return
	 * 
	 * 查询菜单列表分页
	 */
	public Map<String, Object> queryMenuListByPage(Map<String, Object> params) {
        List<Map<String,Object>> items = menuMapper.queryMenuList(params);
        Integer count = menuMapper.queryMenuListCount(params);
        Map<String, Object> pages = Maps.newHashMap();
        pages.put(PageUtil.ITEMS, items);
        pages.put(PageUtil.TOTALS, count);
		return pages;
	}
	/**
	 * @param params
	 * @return
	 * 
	 * 查询菜单列表分页
	 */
	public List<Map<String,Object>> queryMenuList(Map<String, Object> params) {
		return menuMapper.queryMenuList(params);
	}
    public Map<String, Object> getMenu(String menuId) {
    	Map<String,Object> map = Maps.newHashMap();
    	map.put("menuNum", menuId);
    	List<Map<String,Object>> list = menuMapper.queryMenuList(map);
        return list.get(0);
    }
    @Transactional(rollbackFor=Exception.class)
	public void deleteMenu(String menuNum) throws Exception {
    	menuMapper.deleteMenu(menuNum);
	}
 
    @Transactional(rollbackFor=Exception.class)
	public void addMenuOrUpdate(Map<String, Object>  menuMap) throws Exception {
		if("1".equals(menuMap.get("type").toString())){
			menuMapper.addMenu(menuMap);
		}else{
			menuMapper.updateMenu(menuMap);
		}
	}
}
